class Tile3dEffect {
  //初始化注入
  init(theCesium) {
    // let customShaderCode = '';
    theCesium.ModelUtility.customShaderCode = '';
    theCesium.ModelUtility.updateForwardAxis = function (theModel) {
      this['_model'] = theModel;
    };
 
    console.log(theCesium.ModelUtility.modifyFragmentShaderForLogDepth);
    theCesium.ModelUtility.modifyFragmentShaderForLogDepth = function (theShader) {
      let shaderFlag = false;
      if (this['_model'] && this['_model']['_resource']['_url']['indexOf']('b3dm') !== -0x1) {
        shaderFlag = true;
      }
      theShader = theCesium['ShaderSource']['replaceMain'](theShader, 'czm_depth_main');
      let shaderBegin = `
              varying vec3 v_stcVertex;
              void main(){
                  czm_depth_main();
          `;
      let shaderEnd = `
                  czm_writeLogDepth();
              }
          `;
      let shaderEmpty = `
              varying vec3 v_stcVertex;
              void main(){
                  czm_depth_main();
                  czm_writeLogDepth();
              }
          `;
      if (shaderFlag) {
        theShader += shaderBegin + theCesium.ModelUtility.customShaderCode + shaderEnd;
      } else {
        theShader += shaderEmpty;
      }
      return theShader;
    };
 
    theCesium.ModelUtility.modifyVertexShaderForLogDepth = function (shaderStr1, shaderStr2) {
      shaderStr1 = theCesium.ShaderSource.replaceMain(shaderStr1, 'czm_depth_main');
      let str1 = 'varying vec3 v_stcVertex;';
      let str2 = 'void main()';
      let str3 = '{';
      let str4 = 'czm_depth_main();';
      let str5 = 'v_stcVertex = a_position;';
      let str6 = 'czm_vertexLogDepth(';
      let str7 = '); ';
      let str8 = '} ';
      shaderStr1 += str1 + str2 + str3 + str4 + str5 + str6 + shaderStr2 + str7 + str8;
      return shaderStr1;
    };
 
  }
 
  //更新shader的字符串
  updateShader(theCesium, shaderStr, layerKey){
    theCesium.ModelUtility.customShaderCode = shaderStr;
    theCesium.ModelUtility.renderLayerKey = layerKey;
  }
}
 
const tile3dEffect  = new Tile3dEffect();
export default tile3dEffect;